﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using KPIS.DBM;
using KPIS.GERP.GHRMS.IDAL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class MasEducationQualificationDAL : IMasEducationQualificationDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public MasEducationQualificationDAL()
        {

        }
        public DataSet LoadEducationQualifications(MasEducationQualificationInfo quaInfo)
        {
            string strSQL;
            Nullable<int> EduDegree = 0;
            if (quaInfo.EducationDegree != null)
                EduDegree = quaInfo.EducationDegree.Sequence;

            DataSet ds = null;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Sequence", quaInfo.Sequence);
                dbManager.AddParameters(2, "@EduDegree", EduDegree);
                dbManager.AddParameters(3, "@Filter", quaInfo.strFilter);

                strSQL = "SELECT q.EDU_QUAL_SEQ, q.EDU_QUAL_CODE, q.EDU_QUAL_NAME, q.EDU_QUAL_NAME_ENG"
                        + ", q.EDU_QUAL_NAME_SHORT, q.EDU_DEGREE_SEQ, d.EDU_DEGREE_NAME"
                        + ", q.RECORD_STATUS, q.CREATED_BY, q.UPDATED_BY"
                    + " FROM MAS_EDU_QUALIFICATIONS q"
                        + " LEFT JOIN MAS_EDU_DEGREES d ON q.EDU_DEGREE_SEQ = d.EDU_DEGREE_SEQ"
                    + " WHERE q.RECORD_STATUS not like @DeleteRecord"
                    + (quaInfo.Sequence > 0 ? " AND q.EDU_QUAL_SEQ = @Sequence" : "")
                    + (EduDegree > 0 ? " AND q.EDU_DEGREE_SEQ = @EduDegree" : "")
                    //+ " AND CASE WHEN @Filter = '' THEN 0 = 0"
                    //    + " ELSE (q.EDU_QUAL_NAME LIKE CONCAT('%', @Filter , '%')"
                    //                + " OR d.EDU_DEGREE_NAME LIKE CONCAT('%', @Filter , '%')"
                    //    + ") END";
                    + " and q.EDU_QUAL_NAME LIKE CONCAT('%', @Filter , '%') OR d.EDU_DEGREE_NAME LIKE CONCAT('%', @Filter , '%')";
                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
                
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
            return ds;
        }
        public IList<MasEducationQualificationInfo> ListEducationQualifications(MasEducationQualificationInfo quaInfo)
        {
            string strSQL;
            Nullable<int> EduDegree = 0;
            if (quaInfo.EducationDegree != null)
                EduDegree = quaInfo.EducationDegree.Sequence;

            List<MasEducationQualificationInfo> infoList = new List<MasEducationQualificationInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Seq", quaInfo.Sequence);
                dbManager.AddParameters(2, "@EduDegree", EduDegree);

                strSQL = "SELECT EDU_QUAL_SEQ, EDU_QUAL_CODE, EDU_QUAL_NAME, EDU_QUAL_NAME_ENG"
                    + ", EDU_QUAL_NAME_SHORT, EDU_DEGREE_SEQ, RECORD_STATUS, CREATED_BY, UPDATED_BY"
                    + " FROM MAS_EDU_QUALIFICATIONS "
                    + " WHERE RECORD_STATUS not like @DeleteRecord"
                    + (quaInfo.Sequence > 0 ? " AND EDU_QUAL_SEQ = @Seq" : "")
                    + (EduDegree > 0 ? " AND EDU_DEGREE_SEQ = @EduDegree" : "");

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    MasEducationDegreeDAL degreeDAL = new MasEducationDegreeDAL();
                    MasEducationDegreeInfo degreeInfo = new MasEducationDegreeInfo();
                    IList<MasEducationDegreeInfo> degreeList = degreeDAL.ListEducationDegree(new MasEducationDegreeInfo(rdr.IsDBNull(5) ? 0 : rdr.GetInt32(5)));
                    if (degreeList.Count > 0)
                        degreeInfo = degreeList[0];

                    MasEducationQualificationInfo Info = new MasEducationQualificationInfo(rdr.IsDBNull(0) ? (int?)null : rdr.GetInt32(0)
                            , rdr.IsDBNull(1) ? "" : rdr.GetString(1)    
                            , rdr.IsDBNull(2) ? "" : rdr.GetString(2)
                            , rdr.IsDBNull(3) ? "" : rdr.GetString(3)
                            , rdr.IsDBNull(4) ? "" : rdr.GetString(4)
                            , degreeInfo                            
                            , rdr.IsDBNull(6) ? "" : rdr.GetString(6)
                            , rdr.IsDBNull(7) ? (int?)null : rdr.GetInt32(7)
                            , rdr.IsDBNull(8) ? (int?)null : rdr.GetInt32(8));
                    infoList.Add(Info);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
            return infoList;
        }

        public void Insert(MasEducationQualificationInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(8);

                dbManager.AddParameters(0, "@Code", Info.Code);
                dbManager.AddParameters(1, "@Name", Info.Name);
                dbManager.AddParameters(2, "@NameEng", Info.NameEng);
                dbManager.AddParameters(3, "@NameShort", Info.NameShort);
                dbManager.AddParameters(4, "@EduDegree", Info.EducationDegree == null ? (int?)null : Info.EducationDegree.Sequence);
                dbManager.AddParameters(5, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(6, "@CreatedBy", Info.CreatedBy);
                dbManager.AddParameters(7, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO MAS_EDU_QUALIFICATIONS"
                    + "(EDU_QUAL_CODE, EDU_QUAL_NAME, EDU_QUAL_NAME_ENG, EDU_QUAL_NAME_SHORT, EDU_DEGREE_SEQ"
                        + ", RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES (@Code, @Name, @NameEng, @NameShort, @EduDegree, @RecordStatus, @CreatedBy, @CreatedWhen)";
                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(MasEducationQualificationInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(8);

                dbManager.AddParameters(0, "@Sequence", Info.Sequence);
                dbManager.AddParameters(1, "@Code", Info.Code);
                dbManager.AddParameters(2, "@Name", Info.Name);
                dbManager.AddParameters(3, "@NameEng", Info.NameEng);
                dbManager.AddParameters(4, "@NameShort", Info.NameShort);
                dbManager.AddParameters(5, "@EduDegree", Info.EducationDegree == null ? (int?)null : Info.EducationDegree.Sequence);
                dbManager.AddParameters(6, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(7, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE MAS_EDU_QUALIFICATIONS"
                            + " SET EDU_QUAL_CODE = @Code"
                            + ", EDU_QUAL_NAME = @Name"
                            + ", EDU_QUAL_NAME_ENG = @NameEng"
                            + ", EDU_QUAL_NAME_SHORT = @NameShort"
                            + ", EDU_DEGREE_SEQ = @EduDegree"
                            + ", UPDATED_BY = @UpdatedBy"
                            + ", UPDATED_WHEN = @UpdatedWhen"
                        + " WHERE EDU_QUAL_SEQ = @Sequence";
                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@RecordStatus", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(3, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE MAS_EDU_QUALIFICATIONS"
                        + " SET RECORD_STATUS = @RecordStatus"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                        + " WHERE EDU_QUAL_SEQ = @Sequence";
                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
